View Javadoc

1   package org.apache.maven.surefire.junitcore;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Properties;
25  
26  import org.apache.maven.surefire.booter.ProviderParameterNames;
27  
28  /**
29   * @author Kristian Rosenvold
30   */
31  public final class JUnitCoreParameters
32  {
33      public static final String PARALLEL_KEY = ProviderParameterNames.PARALLEL_PROP;
34  
35      public static final String PERCORETHREADCOUNT_KEY = "perCoreThreadCount";
36  
37      public static final String THREADCOUNT_KEY = ProviderParameterNames.THREADCOUNT_PROP;
38  
39      public static final String THREADCOUNTSUITES_KEY = ProviderParameterNames.THREADCOUNTSUITES_PROP;
40  
41      public static final String THREADCOUNTCLASSES_KEY = ProviderParameterNames.THREADCOUNTCLASSES_PROP;
42  
43      public static final String THREADCOUNTMETHODS_KEY = ProviderParameterNames.THREADCOUNTMETHODS_PROP;
44  
45      public static final String USEUNLIMITEDTHREADS_KEY = "useUnlimitedThreads";
46  
47      public static final String PARALLEL_TIMEOUT_KEY = ProviderParameterNames.PARALLEL_TIMEOUT_PROP;
48  
49      public static final String PARALLEL_TIMEOUTFORCED_KEY = ProviderParameterNames.PARALLEL_TIMEOUTFORCED_PROP;
50  
51      public static final String PARALLEL_OPTIMIZE_KEY = ProviderParameterNames.PARALLEL_OPTIMIZE_PROP;
52  
53      private final String parallel;
54  
55      private final Boolean perCoreThreadCount;
56  
57      private final int threadCount;
58  
59      private final int threadCountSuites;
60  
61      private final int threadCountClasses;
62  
63      private final int threadCountMethods;
64  
65      private final double parallelTestsTimeoutInSeconds;
66  
67      private final double parallelTestsTimeoutForcedInSeconds;
68  
69      private final Boolean useUnlimitedThreads;
70  
71      private final boolean parallelOptimization;
72  
73      public JUnitCoreParameters( Properties properties )
74      {
75          parallel = properties.getProperty( PARALLEL_KEY, "none" ).toLowerCase();
76          perCoreThreadCount = Boolean.valueOf( properties.getProperty( PERCORETHREADCOUNT_KEY, "true" ) );
77          threadCount = Integer.valueOf( properties.getProperty( THREADCOUNT_KEY, "0" ) );
78          threadCountMethods = Integer.valueOf( properties.getProperty( THREADCOUNTMETHODS_KEY, "0" ) );
79          threadCountClasses = Integer.valueOf( properties.getProperty( THREADCOUNTCLASSES_KEY, "0" ) );
80          threadCountSuites = Integer.valueOf( properties.getProperty( THREADCOUNTSUITES_KEY, "0" ) );
81          useUnlimitedThreads = Boolean.valueOf( properties.getProperty( USEUNLIMITEDTHREADS_KEY, "false" ) );
82          parallelTestsTimeoutInSeconds =
83              Math.max( Double.valueOf( properties.getProperty( PARALLEL_TIMEOUT_KEY, "0" ) ), 0 );
84          parallelTestsTimeoutForcedInSeconds =
85              Math.max( Double.valueOf( properties.getProperty( PARALLEL_TIMEOUTFORCED_KEY, "0" ) ), 0 );
86          parallelOptimization = Boolean.valueOf( properties.getProperty( PARALLEL_OPTIMIZE_KEY, "true" ) );
87      }
88  
89      private static Collection<String> lowerCase( String... elements )
90      {
91          ArrayList<String> lowerCase = new ArrayList<String>();
92          for ( String element : elements )
93          {
94              lowerCase.add( element.toLowerCase() );
95          }
96          return lowerCase;
97      }
98  
99      private boolean isAllParallel()
100     {
101         return "all".equals( parallel );
102     }
103 
104     public boolean isParallelMethods()
105     {
106         return isAllParallel() || lowerCase( "both", "methods", "suitesAndMethods", "classesAndMethods" ).contains(
107             parallel );
108     }
109 
110     public boolean isParallelClasses()
111     {
112         return isAllParallel() || lowerCase( "both", "classes", "suitesAndClasses", "classesAndMethods" ).contains(
113             parallel );
114     }
115 
116     public boolean isParallelSuites()
117     {
118         return isAllParallel() || lowerCase( "suites", "suitesAndClasses", "suitesAndMethods" ).contains( parallel );
119     }
120 
121     /**
122      * @deprecated Instead use the expression ( {@link #isParallelMethods()} && {@link #isParallelClasses()} ).
123      */
124     @Deprecated
125     public boolean isParallelBoth()
126     {
127         return isParallelMethods() && isParallelClasses();
128     }
129 
130     public Boolean isPerCoreThreadCount()
131     {
132         return perCoreThreadCount;
133     }
134 
135     public int getThreadCount()
136     {
137         return threadCount;
138     }
139 
140     public int getThreadCountMethods()
141     {
142         return threadCountMethods;
143     }
144 
145     public int getThreadCountClasses()
146     {
147         return threadCountClasses;
148     }
149 
150     public int getThreadCountSuites()
151     {
152         return threadCountSuites;
153     }
154 
155     public Boolean isUseUnlimitedThreads()
156     {
157         return useUnlimitedThreads;
158     }
159 
160     public double getParallelTestsTimeoutInSeconds()
161     {
162         return parallelTestsTimeoutInSeconds;
163     }
164 
165     public double getParallelTestsTimeoutForcedInSeconds()
166     {
167         return parallelTestsTimeoutForcedInSeconds;
168     }
169 
170     public boolean isNoThreading()
171     {
172         return !isParallelismSelected();
173     }
174 
175     public boolean isParallelismSelected()
176     {
177         return isParallelSuites() || isParallelClasses() || isParallelMethods();
178     }
179 
180     public boolean isParallelOptimization()
181     {
182         return parallelOptimization;
183     }
184 
185     @Override
186     public String toString()
187     {
188         return "parallel='" + parallel + '\'' + ", perCoreThreadCount=" + perCoreThreadCount + ", threadCount="
189             + threadCount + ", useUnlimitedThreads=" + useUnlimitedThreads + ", threadCountSuites=" + threadCountSuites
190             + ", threadCountClasses=" + threadCountClasses + ", threadCountMethods=" + threadCountMethods
191             + ", parallelOptimization=" + parallelOptimization;
192     }
193 }